tool
extends StaticBody2D

enum Items {barrelBlack_side, barrelBlack_top, barrelGreen_side,
			barrelGreen_top, barrelRed_side, barrelRed_top, barrelRust_side,
			barrelRust_top, barricadeMetal, barricadeWood, fenceRed, fenceYellow,
			sandbagBeige, sandbagBeige_open, sandbagBrown, sandbagBrown_open,
			treeBrown_large, treeBrown_small, treeGreen_large, treeGreen_small, treeGreen_twigs, treeBrown_twigs, wireCrooked, wireStraight}
			
enum CTypes {full_solid, half_solid, not_solid}
#
			
var regions = {
	Items.barrelBlack_side: Rect2(532, 90, 56, 40),
	Items.barrelBlack_top: Rect2(220, 89, 48, 48),
	Items.barrelGreen_side: Rect2(476, 90, 56, 40),
	Items.barrelGreen_top: Rect2(220, 137, 48, 48),
	Items.barrelRed_side: Rect2(420, 94, 56, 40),
	Items.barrelRed_top: Rect2(172, 89, 48, 48),
	Items.barrelRust_side: Rect2(588, 90, 56, 40),
	Items.barrelRust_top: Rect2(172, 137, 48, 48),
	Items.barricadeMetal: Rect2(532, 130, 56, 56),
	Items.barricadeWood: Rect2(72, 130, 56, 56),
	Items.fenceRed: Rect2(336, 443, 32, 96),
	Items.fenceYellow: Rect2(216, 550, 32, 104),
	Items.sandbagBeige: Rect2(164, 282, 44, 64),
	Items.sandbagBeige_open: Rect2(518, 350, 55, 84),
	Items.sandbagBrown: Rect2(622, 278, 44, 64),
	Items.sandbagBrown_open: Rect2(596, 450, 55, 84),
	Items.treeBrown_large: Rect2(0, 654, 128, 128),
	Items.treeBrown_small: Rect2(694, 118, 72, 72),
	Items.treeGreen_large: Rect2(128, 654, 128, 128),
	Items.treeGreen_small: Rect2(694, 190, 72, 72),
	Items.treeGreen_twigs: Rect2(644, 134, 44, 52),
	Items.treeBrown_twigs: Rect2(128, 133, 44, 52),
	Items.wireCrooked: Rect2(0, 782-346-88, 95, 88),
	Items.wireStraight: Rect2(0, 782-688-33, 139, 33)
}
# a, b, c, d
# b, 782-a-c   d, c
export (Items) var type setget _update
export (CTypes) var ctype setget _update_ctype

onready var sprite := $Sprite

func _ready():
	_update(type)

func _update(_type):
	type = _type
	#如果不是在编辑器里运行，那么就得等到该节点进入到场景树后在运行下面的代码
	if not Engine.is_editor_hint():
		yield(self, 'ready')
	if sprite:	#这里主要是防止godot哔哔说找不到$Sprite
		sprite.region_rect = regions[type]
		var rect = RectangleShape2D.new()
		rect.extents = sprite.region_rect.size / 2
		$CollisionShape2D.shape = rect
		
func _update_ctype(value):
	ctype = value
	if not Engine.is_editor_hint():
		yield(self, "ready")
	collision_layer = 0
	if value == CTypes.full_solid:
		collision_layer = 1
	elif value == CTypes.half_solid:
		collision_layer = 16
		
